package com.yisu.auction.api.web.controller.operatorBusiness;

import com.yisu.auction.api.core.service.IMobileRechargeService;
import com.yisu.auction.api.core.service.impl.func.MobileChargeUtil;
import com.yisu.auction.api.domain.request.operatorBusiness.TelephoneChargeRequest;
import com.yisu.auction.api.domain.vo.configJsonVos.TrafficJsonVo;
import com.yisu.auction.api.web.controller.BaseController;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.api.web.utils.PingppUtil;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.MobileRecharge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * author: 赵杰
 * Business Description： 手机流量业务
 * createTime：2017/9/1
 */
@Controller
@RequestMapping(value = "/mobileFlow")
public class MobileFlowController extends BaseController {
    @Resource
    private IMobileRechargeService iMobileRechargeService;
    @Resource
    private MobileChargeUtil mobileChargeUtil;
    /**
     * author: 赵杰
     * Business Description：获取首页相关业务信息
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/rechargeOrder", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult recharge(TelephoneChargeRequest telephoneChargeRequest) throws UnsupportedEncodingException, InterruptedException {
        if (StringUtils.isEmpty(telephoneChargeRequest.getServicenumber()) || StringUtils.isEmpty(telephoneChargeRequest.getId()) || StringUtils.isEmpty(telephoneChargeRequest.getPkgtype())) {
            return new JsonResult(ResponseCode.FAILURE.code, "参数为空");
        }
        TrafficJsonVo trafficJsonVo = mobileChargeUtil.getTrafficJson(telephoneChargeRequest.getId());
        if (trafficJsonVo == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "充值选项不存在！");
        }
        double amount = 0;
        if (MobileRecharge.PkgTypeEnum.NationWide.code == Integer.valueOf(telephoneChargeRequest.getPkgtype())) {
            amount = trafficJsonVo.getNationWide().getAmount();
        } else {
            amount = trafficJsonVo.getProvince().getAmount();
        }
        //创建充值订单
        String orderNo = iMobileRechargeService.addMobileRecharge(telephoneChargeRequest);
        String chargeStr = PingppUtil.getChargeCommon(orderNo, amount + "", telephoneChargeRequest.getPayType(), telephoneChargeRequest.getCallbackUrl(),telephoneChargeRequest.getAppSign());
        if (!org.apache.commons.lang.StringUtils.isEmpty(chargeStr)) {
            JsonResult result = new JsonResult();
            result.setData(chargeStr);
            return result;
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }
}
